package site.isscloud.project.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.isscloud.project.domain.PjLocation;
import site.isscloud.project.dto.TemperatureTrendDTO;
import site.isscloud.project.mapper.PjLocationMapper;
import site.isscloud.project.mapper.PjStatsLocationTemperatureHourMapper;
import site.isscloud.project.mapper.PjStatsLocationTemperatureMinuteMapper;
import site.isscloud.project.service.TemperatureTrendService;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class TemperatureTrendServiceImpl implements TemperatureTrendService {

    @Autowired
    private PjStatsLocationTemperatureMinuteMapper minuteMapper;

    @Autowired
    private PjStatsLocationTemperatureHourMapper hourMapper;

    @Autowired
    private PjLocationMapper pjLocationMapper;

    @Override
    public List<Map<String, Object>> getHourlyTrendByElevatorCode(String elevatorCode, LocalDateTime startTime, LocalDateTime endTime) {
        return minuteMapper.selectTrendByElevatorCodeAndTimeRange(elevatorCode, startTime, endTime, "minute");
    }

    @Override
    public List<Map<String, Object>> getDailyTrendByElevatorCode(String elevatorCode, LocalDateTime startTime, LocalDateTime endTime) {
        return hourMapper.selectTrendByElevatorCodeAndTimeRange(elevatorCode, startTime, endTime, "hour");
    }

    @Override
    public List<TemperatureTrendDTO> getHourlyTrendByLcId(String lcId, LocalDateTime startTime, LocalDateTime endTime) {
        return minuteMapper.selectTrendByLcIdAndTimeRange(lcId, startTime, endTime);
    }

    @Override
    public List<TemperatureTrendDTO> getDailyTrendByLcId(String lcId, LocalDateTime startTime, LocalDateTime endTime) {
        return hourMapper.selectTrendByLcIdAndTimeRange(lcId, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> getLatestTemperatureByLcId(String lcIds) {
        List<Map<String, Object>> result = new ArrayList<>();

        lcIds = lcIds.replace("\"", "");
        Long lcId = Long.parseLong(lcIds);
        // 获取电梯信息
        PjLocation elevator = pjLocationMapper.selectPjLocationByLcId(lcId);
        if (elevator == null) {
            return result;
        }

        Integer startMeter = elevator.getStartMeter();
        Integer endMeter = elevator.getEndMeter();

        if (startMeter == null || endMeter == null) {
            return result;
        }

        // 获取最新的温度数据
        LocalDateTime recentTime = LocalDateTime.now().minusMinutes(5); // 获取最近5分钟内的数据

        List<TemperatureTrendDTO> recentData = minuteMapper.selectTrendByLcIdAndTimeRange(
                lcIds, recentTime, LocalDateTime.now());

        if (!recentData.isEmpty()) {
            // 使用最新的数据
            TemperatureTrendDTO latestData = recentData.get(recentData.size() - 1);

            // 这里需要根据你的实际数据结构来解析温度数据
            // 假设每个数据点对应一个米标
            for (int meter = startMeter; meter <= endMeter; meter++) {
                Map<String, Object> tempPoint = new HashMap<>();
                tempPoint.put("meter", meter);
                // 这里需要根据实际数据调整温度值的获取方式
                tempPoint.put("temperature", latestData.getTemperature());
                result.add(tempPoint);
            }
        }

        return result;
    }

    // 添加工具方法用于 Date 到 LocalDateTime 的转换
    private LocalDateTime convertToLocalDateTime(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }

    // 为兼容旧代码添加的重载方法
    public List<Map<String, Object>> getHourlyTrendByElevatorCode(String elevatorCode, Date startTime, Date endTime) {
        return getHourlyTrendByElevatorCode(elevatorCode, convertToLocalDateTime(startTime), convertToLocalDateTime(endTime));
    }

    public List<Map<String, Object>> getDailyTrendByElevatorCode(String elevatorCode, Date startTime, Date endTime) {
        return getDailyTrendByElevatorCode(elevatorCode, convertToLocalDateTime(startTime), convertToLocalDateTime(endTime));
    }

    public List<TemperatureTrendDTO> getHourlyTrendByLcId(String lcId, Date startTime, Date endTime) {
        return getHourlyTrendByLcId(lcId, convertToLocalDateTime(startTime), convertToLocalDateTime(endTime));
    }

    public List<TemperatureTrendDTO> getDailyTrendByLcId(String lcId, Date startTime, Date endTime) {
        return getDailyTrendByLcId(lcId, convertToLocalDateTime(startTime), convertToLocalDateTime(endTime));
    }

    @Override
    public List<Map<String, Object>> getDailyTrendByElevatorCodeFromHourTable(String elevatorCode, LocalDateTime startTime, LocalDateTime endTime) {
        // 从天趋势查询改为从小时统计表查询
        return hourMapper.selectDailyTrendByElevatorCode(elevatorCode, startTime, endTime);
    }
}